﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XbdSoft.WPFVersion.ViewModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ServiceLocation;
using XbdSoft.Domain;
using XbdSoft.WPFVersion.Common;

namespace XbdSoft.WPFVersion.View
{
    /// <summary>
    /// Interaction logic for DownloadView.xaml
    /// </summary>
    public partial class DownloadView : UserControl
    {
        #region Fields
        protected readonly IEventAggregator EventAggregator = null;
        protected readonly IUnityContainer Container = null;
        #endregion

        #region Ctor
        private readonly DownloadViewModel ViewModel = null;
        public DownloadView()
        {
            EventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            Container = ServiceLocator.Current.GetInstance<IUnityContainer>();
            InitializeComponent();
            ViewModel = Container.Resolve<DownloadViewModel>();
            ContinueDownloadCommand = ViewModel.ContinueDownloadCommand;
            DataContext = ViewModel; 
        }

        #endregion

        #region Download From External

        public void Download(AudioResource audioResource)
        { 
            ViewModel.AddDownload(audioResource);
        }

        #endregion

        public ICommand ContinueDownloadCommand { get; set; }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        { 
        }

        private void ContinueDownload_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            var parent = LogicalTreeHelper.GetParent(menuItem);
            var target = ContextMenuService.GetPlacementTarget(parent);
            var downloadDTO = (target as Border).DataContext as DownloadDTO;
            if (downloadDTO != null)
            {
                ViewModel.AddDownload(downloadDTO);
            } 
        }

        private void DeleteDownlaod_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            var parent = LogicalTreeHelper.GetParent(menuItem);
            var target = ContextMenuService.GetPlacementTarget(parent);
            var downloadDTO = (target as Border).DataContext as DownloadDTO;
            if (downloadDTO != null)
            {
                ViewModel.DeleteDownload(downloadDTO);
            } 
        }

        private void PauseDownload_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            var parent = LogicalTreeHelper.GetParent(menuItem);
            var target = ContextMenuService.GetPlacementTarget(parent);
            var downloadDTO = (target as Border).DataContext as DownloadDTO;
            if (downloadDTO != null)
            {
                ViewModel.PauseDownload(downloadDTO);
            } 
        }
    }
}
